﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScriptCompare
{
    public struct ScriptLine
    {
        private int _lineHash;
        private string _lineText;
        private bool _lineEmpty;
        private int _lineNumber;
        private bool _highlightLine = false;

        public ScriptLine(string text, int lineNumber)
        {
            _lineEmpty = String.IsNullOrEmpty(text);
            _lineNumber = lineNumber;

            if (!_lineEmpty)
                _lineHash = HashString(text.ToLower());
            else
                _lineHash = 0;

            _lineText = text;
        }

        public bool LineHighlighted
        {
            get { return _highlightLine; }
        }

        public void HighlightLine()
        {
            _highlightLine = true;
        }

        public override int GetHashCode()
        {
            return (_lineHash);
        }

        public bool Matches(ScriptLine line)
        {
            return (line._lineHash == this._lineHash);
        }

        public int LineNumber
        {
            get { return _lineNumber; }
        }

        public string LineText
        {
            get { return _lineText; }
        }

        public bool IsComment
        {
            get { return (_lineText.StartsWith("'")); }
        }

        public bool IsEmpty
        {
            get { return String.IsNullOrEmpty(_lineText); }
        }

        public static bool operator ==(ScriptLine a, ScriptLine b)
        {
            return (a._lineHash == b._lineHash);
        }

        public static bool operator !=(ScriptLine a, ScriptLine b)
        {
            return (a._lineHash != b._lineHash);
        }

        public static int HashString(string input)
        {
            const uint p = 16777619;

            uint hash = 2166136261;
            foreach (char b in input)
                hash = (hash ^ b) * p;

            hash += hash << 13;
            hash ^= hash >> 7;
            hash += hash << 3;
            hash ^= hash >> 17;
            hash += hash << 5;

            return (int)hash;            
        }
    }
}
